package com.bw.a1908.uitls.gfit.display;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.bw.a1908.uitls.gfit.GiftBean;
import com.bw.a1908.uitls.gfit.impl.GiftFactory;
import com.bw.a1908.uitls.gfit.impl.GiftRunStage;
import com.bw.a1908.uitls.gfit.impl.IGiftView;

import java.util.ArrayList;
import java.util.List;

/**
 * 左侧展示区域
 */
public class LiftDisplayView extends LinearLayout implements IDisplayView, DisplayController.OnGiftChanger {

    private DisplayController controller;

    public LiftDisplayView(Context context) {
        super(context);
        init();
    }

    public LiftDisplayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOrientation(LinearLayout.VERTICAL);
        controller = new DisplayController(3);
        controller.setOnGiftChanger(this);
    }

    @Override
    public void showView(GiftBean giftBean) {
        controller.addGift(giftBean);
    }

    @Override
    public IGiftView nextView(GiftBean giftBean) {
        return null;
    }


    @Override
    public void show(GiftBean bean) {
        IGiftView iGiftView = GiftFactory.getInstance().create(getContext(), bean);
        View view = iGiftView.onCreateView(bean);
        view.setTag(bean);
        addView(view);
        iGiftView.onRunning(GiftRunStage.IN);
    }

    @Override
    public void finish(GiftBean bean) {
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view.getTag() == bean) {
                IGiftView giftView = (IGiftView) view;
                giftView.onRunning(GiftRunStage.OUT);
                //这需添加回调
                removeView(view);
            }
        }
    }
}
